<template>
    <div class="page-container">
        <div class="card split">
            <div>
                <el-input clearable @clear="load()" class="card-input" v-model="data.searchData.categoryName"
                    placeholder="请输入名称查询" prefix-icon="Search" @keyup.enter="load()"></el-input>
                <el-input clearable @clear="load()" class="card-input" v-model="data.searchData.categoryAlias"
                    placeholder="请输入别名查询" prefix-icon="Search" @keyup.enter="load()"></el-input>
                <el-button class="card-button" @click="load()" type="primary">查 询</el-button>
                <el-button class="card-button" @click="resetSearchData()">重 置</el-button>
            </div>
            <div>
                <el-button class="card-button" type="primary"
                    @click="dialogData.dialogVisible = true; dialogData.title = '新增分类'; resetCategoryModel()">新增分类</el-button>
            </div>
        </div>
        <div class="card">
            <el-table :data="tableData" style="width: 100%;" max-height="65vh">
                <el-table-column type="selection" width="55" />
                <el-table-column label="序号" width="100" type="index" />
                <el-table-column label="所属用户名称" prop="nickName" v-if="userStore.imAdmin()"/>
                <el-table-column label="所属用户id" prop="createId"v-if="userStore.imAdmin()"/>
                <el-table-column label="分类名称" prop="categoryName" />
                <el-table-column label="分类别名" prop="categoryAlias" />
                <el-table-column label="操作" width="100">
                    <template #default="scope">
                        <el-button style="margin-right: 5px;" type="primary" icon="Edit" circle plain
                            @click="handleEdit(scope.row)"></el-button>
                        <el-button type="danger" icon="Delete" circle plain
                            @click="handleDel(scope.row.id)"></el-button>
                    </template>
                </el-table-column>
                <template #empty>
                    <el-empty style="height: 100%;" description="没有查询到数据" />
                </template>
            </el-table>
        </div>
        <div class="card">
            <el-pagination v-model:current-page="paginationData.pageNum" v-model:page-size="paginationData.pageSize"
                :page-sizes="[10, 15, 20, 50]" layout="total, sizes, prev, pager, next, jumper"
                :total="paginationData.total" @change="load()" />
        </div>
        <el-dialog v-model="dialogData.dialogVisible" :title="dialogData.title" width="30%">
            <el-form :model="categoryModel" :rules="dialogData.rules" label-width="100px" style="padding-right: 30px;">
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="分类别名" prop="categoryAlias">
                    <el-input v-model="categoryModel.categoryAlias" minlength="1" maxlength="10"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="center">
                    <el-button @click="dialogData.dialogVisible = false; resetCategoryModel()">取消</el-button>
                    <el-button @click="categoryModel.id == '' ? addCategory() : updateCategory()"
                        type="primary">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>



<script setup>
import { reactive, ref } from 'vue';
import {
    articleCategoryPageService,
    articleCategoryAddService,
    articleCategoryUpdateService,
    articleCategoryDeleteByIdService
} from '@/api/article';
import { ElMessage } from 'element-plus';
import { ElMessageBox } from 'element-plus';
import { useUserStore } from '@/store/user';

const userStore = useUserStore()

const data = ref({
    searchData: {
        id: '',
        categoryName: '',
        categoryAlias: '',
    },
    category: {
        id: 1,
        categoryName: "",
        categoryAlias: ""
    }
})

const tableData = ref([])

const dialogData = ref({
    dialogVisible: false,
    title: '',
    rules: {
        categoryName: [
            { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        categoryAlias: [
            { required: true, message: '请输入别名', trigger: 'blur' },
        ]
    }
})

const categoryModel = ref({
    id: '',
    categoryName: '',
    categoryAlias: ''
})

const resetCategoryModel = () => {
    categoryModel.value.id = ''
    categoryModel.value.categoryName = null
    categoryModel.value.categoryAlias = null
}

// 分页数据
const paginationData = ref({
    pageNum: 1,
    pageSize: 10,
    total: 0
})

const articleCategoryList = async () => {
    if(userStore.imSuperAdmin()) {
        data.value.searchData.id = null;
    } else {
        data.value.searchData.id = userStore.getUser().id
    }
    const params = {
        pageNum: paginationData.value.pageNum,
        pageSize: paginationData.value.pageSize,
        createId: data.value.searchData.id,
        categoryName: data.value.searchData.categoryName,
        categoryAlias: data.value.searchData.categoryAlias
    }
    let result = await articleCategoryPageService(params);
    tableData.value = result.list //分页数据使用的是pageinfo对象，数据存储在list里面
    paginationData.value.total = result.total
    resetCategoryModel()
}
const resetSearchData = () => {
    data.value.searchData.categoryName = ''
    data.value.searchData.categoryAlias = ''
    load()
}

const load = () => {
    articleCategoryList()
}
load()

const handleEdit = (row) => {
    dialogData.value.title = '编辑分类'
    dialogData.value.dialogVisible = true
    categoryModel.value.id = row.id
    categoryModel.value.categoryName = row.categoryName
    categoryModel.value.categoryAlias = row.categoryAlias
}

const addCategory = async () => {
    await articleCategoryAddService(categoryModel.value)
    ElMessage.success('添加成功')
    load()
    dialogData.value.dialogVisible = false
}

const updateCategory = async () => {
    await articleCategoryUpdateService(categoryModel.value)
    ElMessage.success('更新成功')
    load()
    dialogData.value.dialogVisible = false
}


//删除分类
const handleDel = (row) => {
    ElMessageBox.confirm(
        '确认删除此文章分类？',
        '删除确认',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        let result = await articleCategoryDeleteByIdService(row)
        ElMessage.success("删除成功")
        //刷新列表
        load()
    })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}
</script>